package com.mxnet.chongjin.supoman.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.bean.RepairRecord;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.TimeUtils;

import java.util.List;

/**
 * author:chongjin
 * email:lcj270018655@163.com
 * date:2018/4/24
 * other:
 */

public class LvOrderApplyingAdapter extends BaseAdapter {
    private Context mContext;
    public List<RepairRecord> mList;

    public LvOrderApplyingAdapter(Context context, List<RepairRecord> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_order_applying, parent, false);
            holder = new ViewHolder();
            holder.tvOrderNum = convertView.findViewById(R.id.tv_item_order_applying_order_num);
            holder.tvMachineType = convertView.findViewById(R.id.tv_item_order_applying_machine_type);
            holder.tvMachineInfo = convertView.findViewById(R.id.tv_item_order_applying_machine_information);
            holder.tvOrderTime = convertView.findViewById(R.id.tv_item_order_applying_order_time);

            holder.ivMachinePic = convertView.findViewById(R.id.iv_item_order_applying_machine_pic);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvOrderNum.setText(mContext.getResources().getString(R.string.repair_num) + mList.get(position).getDeclarationNo());
        holder.tvOrderTime.setText(TimeUtils.stampToDate(mList.get(position).getCreateTime(), "yyyy-MM-dd"));
        holder.tvMachineType.setText(mList.get(position).getRobotNo());
        holder.tvMachineInfo.setText(mList.get(position).getDeclarationMemo());

        if (mList.get(position).getImgResource() != null) {
            Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(position).getImgResource().getUrl()).into(holder.ivMachinePic);
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView tvOrderNum, tvMachineType, tvMachineInfo, tvOrderTime;
        ImageView ivMachinePic;
    }
}
